// ignore_for_file: prefer_const_constructors

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:time_manager/app/modules/add_time/add_time_view.dart';
import 'package:time_manager/app/modules/time_logistic/time_logistic.dart';

// import 'package:plugins/plugins.dart';
import 'package:time_manager/utils/update_app.dart';
import 'package:time_manager/view/current/current_view.dart';
import 'package:time_manager/view/inThePast/past_view.dart';
import 'package:time_manager/view/statistics/statistics_view.dart';

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  //当前选中的标识
  int _currentIndex = 0;

  /// 第一步 定义 ValueNotifier  这里传递的数据类型为 String
  ValueNotifier<int> progress = ValueNotifier<int>(0);
  ValueNotifier<bool> isUpdate = ValueNotifier<bool>(false);
  String _platformVersion = '';
  final PageController _pageController = new PageController();

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance?.addPostFrameCallback((mag) async {
      // showUpdate();
      var flag = await UpdateApp.checkUpdate();
      if (flag) {
        showUpdate();
      }
    });
  }

  void updateVal() {
    progress.value = 30;
  }

  showUpdate() async {
    Get.defaultDialog(
      title: '提示',
      barrierDismissible: false,
      onWillPop: () async {
        return false;
      },
      content: Column(children: [
        ValueListenableBuilder(
          valueListenable: progress,
          builder: (context, value, child) {
            return Row(
              children: [
                Expanded(
                    child: LinearProgressIndicator(
                        value: progress.value / 100,
                        //背景颜色
                        backgroundColor: Colors.grey,
                        //进度颜色
                        valueColor: const AlwaysStoppedAnimation<Color>(
                            Colors.blueAccent))),
                SizedBox(
                  width: 10,
                ),
                Offstage(
                  offstage: !(progress.value > 0),
                  child: Text(progress.value.toStringAsFixed(1)),
                )
              ],
            );
          },
        )
      ]),
      confirm: ValueListenableBuilder(
          valueListenable: isUpdate,
          builder: (context, value, child) {
            return ElevatedButton(
                onPressed: isUpdate.value
                    ? null
                    : () {
                        progress.value = 0;
                        isUpdate.value = true;
                        UpdateApp.download((String val) {
                          int temp = int.tryParse(val)!;
                          progress.value = temp;
                        });
                      },
                child: Text('立即更新'));
          }),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SizedBox(
        width: double.infinity,
        height: double.infinity,
        child: PageView(
          controller: _pageController,
          //不可左右滑动
          physics: const NeverScrollableScrollPhysics(),
          children: [
            // CurrentView(),
            // PastView(),
            // StatisticsView(),
            AddTimeView(),
            TimeLogistic()
          ],
        ),
      ),
      // floatingActionButton: FloatingActionButton(
      //   child: Text('开始更新'),
      //   onPressed: () {
      //     UpdateApp.download((String val) {
      //       print('进度显示======== $val');
      //     });
      //   },
      // ),
      bottomNavigationBar: BottomNavigationBar(
        //当前选中的Item 默认为 0
        currentIndex: _currentIndex,
        //点击回调
        onTap: (int value) {
          setState(() {
            _currentIndex = value;
            _pageController.jumpToPage(value);
          });
        },
        //显示文字
        type: BottomNavigationBarType.fixed,
        //选中的颜色
        // selectedItemColor: Colors.redAccent,
        // //未选中颜色
        // unselectedItemColor: Colors.blueGrey,
        // ignore: prefer_const_literals_to_create_immutables
        items: [
          BottomNavigationBarItem(icon: Icon(Icons.today), label: "今日日程"),
          // BottomNavigationBarItem(icon: Icon(Icons.gradient), label: "所有日程"),
          BottomNavigationBarItem(icon: Icon(Icons.store), label: "统计"),
        ],
      ),
    );
  }
}
